package com.leyou.search.mq;

import com.leyou.common.constants.MQConstants;
import com.leyou.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author BaiZY on 2020/8/31.
 * @version 1.0
 */
@Component
public class ItemListener {

    @Autowired
    private SearchService searchService;


    /**
     * 上架
     * @param spuId
     */
    @RabbitListener(bindings = @QueueBinding(value=@Queue(name = MQConstants.QueueConstants.SEARCH_ITEM_UP,durable = "true"),
            exchange=@Exchange(name= MQConstants.ExchangeConstants.ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key= MQConstants.RoutingKeyConstants.ITEM_UP_KEY
    ))
    public void listenItemUp(Long spuId){
        if (spuId != null){
            searchService.saveGoodsById(spuId);
        }
    }

    /**
     * 下架
     */

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name= MQConstants.QueueConstants.SEARCH_ITEM_DOWN,durable = "false"),
            exchange=@Exchange(name = MQConstants.ExchangeConstants.ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKeyConstants.ITEM_DOWN_KEY
    ))
    public void listenItemDown(Long spuId){
        if (spuId != null){
            searchService.deleteGoodsById(spuId);
        }
    }



}
